#include "dropdown.h"
#include "ui_dropdown.h"
//#include "listitem.h"
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QtMath>
#include <QPainter>
#include <QBitmap>
#include <QPainterPath>

#define SHADOW_WIDTH 0                 // 窗口阴影宽度;
#define TRIANGLE_WIDTH 10               // 小三角的宽度;
#define TRIANGLE_HEIGHT 5              // 小三角的高度;
#define BORDER_RADIUS 5                 // 窗口边角的弧度;

DropDown::DropDown(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DropDown)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    this->setWindowFlags(Qt::FramelessWindowHint |Qt::Window);   //设置无边框窗口 | 子窗口设置为非顶级窗口



    {
        //设置具体阴影
        QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
        shadow_effect->setOffset(0, 0);

        //阴影颜色
        shadow_effect->setColor(QColor(38, 78, 119, 127));
        //阴影半径
        shadow_effect->setBlurRadius(8);
        ui->label->setGraphicsEffect(shadow_effect);
    //    this->setGraphicsEffect(shadow_effect);
    }
    {
        //设置具体阴影
        QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
        shadow_effect->setOffset(0, 1);

        //阴影颜色
        shadow_effect->setColor(QColor(150, 150, 150, 127));
        //阴影半径
        shadow_effect->setBlurRadius(4);
        ui->listWidget->setGraphicsEffect(shadow_effect);
    //    this->setGraphicsEffect(shadow_effect);
    }

    ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //隐藏列滑动条
    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);  //隐藏行滑动条
    ui->listWidget->setFrameShape(QListWidget::NoFrame);//隐藏边框
    ui->listWidget->setFocusPolicy(Qt::NoFocus); //移除焦点

    //设置默认字体
    font_.setFamily(QStringLiteral("Microsoft YaHei"));//设置字体
    font_.setPixelSize(15);//字体大小
    font_.setLetterSpacing(QFont::PercentageSpacing,120);//120字间距，默认100

//    for(int i=0;i<10;i++)
//    {
//        QListWidgetItem *item1 = new QListWidgetItem(ui->listWidget);
//        item1->setSizeHint(QSize(300, 50));
////        item1->setFlags(item1->flags() & ~Qt::ItemIsSelectable);//去掉选中项,需要先移除焦点
//        item1->setFont(font);
//        item1->setText("item1");
//        ui->listWidget->addItem(item1);

//    }
//    ui->listWidget->setCurrentRow(0);//选中第0项


}

DropDown::~DropDown()
{
    delete ui;
}

//设置下拉框大小
void DropDown::size(int w, int h)
{
    int count = ui->listWidget->count();
    int labelh = 4+h+count;
//    if(count == 2)
//    {
//        labelh = 7+h+count;

//    }

    this->resize(w+10,h+30);
    ui->listWidget->resize(w,h);
    ui->label->resize(w,labelh);
//    if(count>=2&&count<5)
//    {
//        ui->label->resize(w,labelh+1);
//    }

    //画成圆形图片
//    QPixmap pixmap=QPixmap::fromImage(mat2QImage(frame));
    int width = w;
    int height = h;
    QSize size(width, height);

    {
        QBitmap mask(size);
        QPainter painter(&mask);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
        painter.setRenderHint(QPainter::SmoothPixmapTransform);
        painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
        painter.setBrush(QColor(0, 0, 0));
        painter.drawRoundedRect(0, 0, size.width(), size.height(), 5, 5);//修改这个值，可以改弧度，和直径相等就是圆形
        ui->listWidget->setMask(mask);
//        ui->listWidget->hide();
    }

    {
        int labelh2 = labelh;
        if(count>=2&&count<5)
        {
            labelh2 = labelh+1;
        }

        QSize size(width, labelh2);
        QBitmap mask(size);
        QPainter painter(&mask);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
        painter.setRenderHint(QPainter::SmoothPixmapTransform);
        painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
        painter.setBrush(QColor(0, 0, 0));

        // 小三角区域;
        m_startX = width/2-5;
        QPolygon trianglePolygon;
        trianglePolygon << QPoint(m_startX, m_triangleHeight + SHADOW_WIDTH);
        trianglePolygon << QPoint(m_startX + m_triangleWidth / 2, SHADOW_WIDTH);
        trianglePolygon << QPoint(m_startX + m_triangleWidth, m_triangleHeight + SHADOW_WIDTH);
        QPainterPath drawPath;
        drawPath.addRoundedRect(QRect(SHADOW_WIDTH, m_triangleHeight + SHADOW_WIDTH+1,\
                                    width - SHADOW_WIDTH * 2, labelh2 - SHADOW_WIDTH * 2 - m_triangleHeight),\
                                    BORDER_RADIUS, BORDER_RADIUS);
        // Rect + Triangle;
        drawPath.addPolygon(trianglePolygon);
        painter.drawPath(drawPath);
        ui->label->setMask(mask);
    }

}

//设置下拉框文本
void DropDown::setTexe(QStringList list)
{
    for(int i=0;i<list.size();i++)
    {
        QListWidgetItem *item1 = new QListWidgetItem(ui->listWidget);
        item1->setSizeHint(QSize(ListWidgetItemW, ListWidgetItemH));
//        item1->setFlags(item1->flags() & ~Qt::ItemIsSelectable);//去掉选中项,需要先移除焦点
        item1->setFont(font_);
        item1->setText(list.at(i));
        ui->listWidget->addItem(item1);
    }
}

//追加文本到下拉框
void DropDown::addText(QString text)
{
    QListWidgetItem *item1 = new QListWidgetItem(ui->listWidget);
    item1->setSizeHint(QSize(ListWidgetItemW, ListWidgetItemH));
    item1->setFont(font_);
    item1->setText(text);
    ui->listWidget->addItem(item1);
//    qDebug()<<__LINE__<<"ListWidgetItemH="<<ListWidgetItemH;
}

//清空下拉框内容
void DropDown::clear()
{
    ui->listWidget->clear();
}

//设置每一项的宽高
void DropDown::ListWidgetItemSize(int w, int h)
{
    ListWidgetItemW = w;
    ListWidgetItemH = h;
}

//获取当前列表项的总数
int DropDown::count()
{
    return ui->listWidget->count();
}

void DropDown::setPitchOn()
{
    ui->listWidget->setCurrentRow(-1);//选中第0项
}

QString DropDown::setCurrentRow(int index)
{
    ui->listWidget->setCurrentRow(index);

    return ui->listWidget->item(index)->text();
}

void DropDown::setCurrentRow(QString data)
{
    for(int i=0;i<ui->listWidget->count();i++)
    {
//        qDebug()<<__LINE__<<ui->listWidget->item(i)->text();
        if(ui->listWidget->item(i)->text() == data)
        {
            ui->listWidget->setCurrentRow(i);

            break;
        }
    }
}

bool DropDown::event(QEvent *event)
{
    if (event->type() == QEvent::ActivationChange)
    {
        if(QApplication::activeWindow() != this)
        {
//            QWidget *parentwidget=this->parentWidget();

//            if((QPushButton *)QApplication::activeWindow() == pushButton_)
//            {
//                this->close();
//                sendDropDown(3, "");
//            }
//            else
            {
                this->close();
                sendDropDown(2, "");
            }

        }
    }
    return QWidget::event(event);
}

void DropDown::on_listWidget_itemClicked(QListWidgetItem *item)
{
    QString text = item->text();
    sendDropDown(1, text);

    this->close();
}


